<?php

namespace App\Api\V2\Controller\Bill\User;


use App\Api\Controllers\BaseController;
use App\Models\Store;
use App\Services\Count\UserBillCountService;
use App\Services\Count\UserService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;

class WxCountController extends BaseController
{
    use ResponseTrait;

    //打标数据 只有总店
    const MARK = [
        101 => 1,
        747 => 4,
        874 => 5,
        1002 => 6
    ];

    //小程序  每日收款统计
    public function staticDailyCount(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
//            if (in_array($agent_id, array_keys(self::MARK))) {
//                $store = Store::query()->where(["ascription" => self::MARK[$agent_id]])->get();
//                list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = [0, 0, 0, 0, 0];
//                $storePids = [];
//                foreach ($store as $key => $val) {
//                    $storePids[] = $val->store_id;
//                }
//                list($profit_column, $fee_column) = ["", ""];
//            } else {
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            $storePids = $request->get("store_pid");
//            }

            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column,
                "store_pid" => $storePids,
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
//                "pay_type" => $request->get("pay_type"),
//                "company" => $request->get("company"),
//                "receive_model" => $request->get("receive_model")
            ];
            $countService = new UserBillCountService();
            $count = $countService->staticDailyGroupCount($where);
            return $this->successResponse("ok", ["data" => $count]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    //小程序  每日收款分组统计
    public function staticDailyCountList(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }

//            if (in_array($agent_id, array_keys(self::MARK))) {
//                $store = Store::query()->where(["ascription" => self::MARK[$agent_id]])->get();
//                list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = [0, 0, 0, 0, 0];
//                $storePids = [];
//                foreach ($store as $key => $val) {
//                    $storePids[] = $val->store_id;
//                }
//                list($profit_column, $fee_column) = ["", ""];
//            } else {
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            $storePids = $request->get("store_pid");
//            }
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column,
                "store_pid" => $storePids,
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
//                "pay_type" => $request->get("pay_type"),
//                "company" => $request->get("company"),
//                "receive_model" => $request->get("receive_model")
            ];
            $countService = new UserBillCountService();
            $count = $countService->staticDailyGroupCountList($where);
            return $this->successResponse("ok", ["data" => $count]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    //小程序 分组数据统计
    public function staticStoreGroupCount(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column,
                "store_pid" => $request->get("store_pid"),
                "store_id" => $request->get("store_id"),
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "cashier_id" => $request->get("merchant_id"),
                "company" => $request->get("company"),
                //                "pay_type" => $request->get("pay_type"),
//                "receive_model" => $request->get("receive_model")
            ];
            $countService = new UserBillCountService();
            $count = $countService->staticStoreGroupCount($where);
            return $this->successResponse("ok", ["data" => $count]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }

    }

    //小程序 分组数据列表统计
    public function staticStoreGroupCountList(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column,
                "page" => $request->get("page", 1),
                "pagesize" => $request->get("pagesize", 20),
                "store_pid" => $request->get("store_pid"),
                "store_id" => $request->get("store_id"),
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "cashier_id" => $request->get("merchant_id"),
//                "pay_type" => $request->get("pay_type"),
                "company" => $request->get("company"),
//                "receive_model" => $request->get("receive_model")
            ];
            //分组列表维度
            $group = $request->get("group");
            switch ($group) {
                case "store_pid":
                    $groupData = ["store_pid"];
                    break;
                case "store_id":
                    $groupData = ["store_pid", "store_id"];
                    break;
                case "merchant_id":
                    $groupData = ["store_pid", "store_id", "cashier_id"];
                    break;
                case "company":
                    $groupData = ["company"];
                    break;
                default:
                    throw new \Exception("请设置分组条件");
            }
            //在这里统计店铺
            $countService = new UserBillCountService();
            $count = $countService->staticStoreGroupCountList($where, $groupData);
            return $this->successResponse("ok", $count);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }

    }

    //收银查看明细
    public function staticBillLogList(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
//                "profit_column" => $profit_column,
//                "fee_column" => $fee_column,
                "page" => $request->get("page", 1),
                "pagesize" => $request->get("pagesize", 20),
                "store_pid" => $request->get("store_pid"),
                "store_id" => $request->get("store_id"),
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "cashier_id" => $request->get("merchant_id"),
//                "pay_type" => $request->get("pay_type"),
                "company" => $request->get("company"),
//                "receive_model" => $request->get("receive_model")
            ];
            //在这里统计店铺
            $countService = new UserBillCountService();
            list($list, $total) = $countService->staticBillLogList($where);
            return $this->successResponse("ok", ["data" => $list, "total" => $total]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    //小程序支付方式统计
    public function payTypeCount(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $agent_id = $user->user_id;  //查找下级所有的数据
            if (!empty($request->get("agent_id"))) {
                $agent_id = $request->get("agent_id");
            }
            $mark = $request->get("mark", 0);
            $userService = new UserService();
            if ($mark == 1) {
                if (!in_array($agent_id, array_keys(self::MARK))) {
                    throw new \Exception("不是打标代理商");
                }
                $store = Store::query()->where(["ascription" => self::MARK[$agent_id]])->get();
                list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = [0, 0, 0, 0, 0];
                $storePids = [];
                foreach ($store as $val) {
                    $storePids[] = $val->store_id;
                }
                list($profit_column, $fee_column) = ["", ""];
            } else {
                list($idValues, $column) = $userService->handleUserAccountRelation((int)$agent_id);
                list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
                $storePids = $request->get("store_pid");
                list($profit_column, $fee_column) = $column;
            }
            $agentList = [];
            if ($request->get("addstoresNum", 0) == 1) {
                $agentList = $userService->getSub($agent_id);
            }
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column,
                "store_pid" => $storePids,
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
//                "pay_type" => $request->get("pay_type"),
//                "company" => $request->get("company"),
//                "cashier" => $request->get("merchant_id"),
//                "receive_model" => $request->get("receive_model")
            ];
            //在这里统计店铺
            $countService = new UserBillCountService();
            list($count, $addstoresNum) = $countService->payTypeCount($where, $agentList);
            $count["addstoresNum"] = $addstoresNum;
            return $this->successResponse("ok", ["data" => $count]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

}